package model;

import graphic.Painter;
import graphic.ParticlePainter;

import java.awt.Color;
import java.awt.Graphics;
import java.util.Observable;

public class Particle extends Observable{
	private MyVector position;
	private MyVector velocity;
	private MyVector acceleration;
	private int age;
	private int life;
	private int radius;
	private int collisionCooldown = 0;
	private Painter painter;
	
	public Particle(int radius){
		position = new MyVector(0,0);
		velocity = new MyVector(0,0);
		acceleration = new MyVector(0,0);
		age = 0;
		life = 0;
		setRadius(radius);
		painter = new ParticlePainter(this);
	}
	
	public void move(double dt){
		position = position.add(velocity.multiply(dt));
        velocity = velocity.add(acceleration.multiply(dt));
        this.setChanged();
        this.notifyObservers("move");
	}
	
	public boolean isDead(){
		if(life == 0){
			return true;
		}else{
			return age > life;
		}
	}
	
	public double distance(Particle p2){
		double p1x = position.getX()+getRadius();
		double p1y = position.getY()+getRadius();
		double p2x = p2.getPosition().getX()+p2.getRadius();
		double p2y = p2.getPosition().getY()+p2.getRadius();
		return Math.sqrt(Math.pow((p1x-p2x),2)+Math.pow((p1y-p2y),2));
	}

	public MyVector getPosition() {
		return position;
	}

	public void setPosition(MyVector position) {
		this.position = position;
	}

	public MyVector getVelocity() {
		return velocity;
	}

	public void setVelocity(MyVector velocity) {
		this.velocity = velocity;
	}

	public MyVector getAcceleration() {
		return acceleration;
	}

	public void setAcceleration(MyVector acceleration) {
		this.acceleration = acceleration;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public int getLife() {
		return life;
	}

	public void setLife(int life) {
		this.life = life;
	}
	
	public void paint(Graphics g){
		painter.paint(g);
	}
	
	public void setColor(Color color){
		painter.setColor(color);
	}
	
	public Color getColor(){
		return painter.getColor();
	}

	public int getRadius() {
		return radius;
	}

	public void setRadius(int radius) {
		this.radius = radius;
	}

	public int getCollisionCooldown() {
		return collisionCooldown;
	}

	public void setCollisionCooldown(int collisionCooldown) {
		this.collisionCooldown = collisionCooldown;
	}
}
